package com.promote.algorithm;

/**
 * @ClassName ReverseWords
 * Description 反正字符串里的单词
 * @Author LiZiHao
 * Date 2022/12/13 15:00
 * @Version 1.0
 **/
public class ReverseWords {

    //数组反转
    public static String reverseWords_1(String s) {
        String[] wordArray = s.split(" ");
        StringBuffer stringBuffer = new StringBuffer();

        int len = wordArray.length;

        while(len-- > 0) {
            if (!wordArray[len].isEmpty()) {
                if (stringBuffer.length() > 0) {
                    stringBuffer.append(" ");
                }
                stringBuffer.append(wordArray[len]);
            }
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) {
        String s = " wang gou dan";
        String res = reverseWords_1(s);
        System.out.println(res);
    }
}
